package copy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import copy.com.R;
import copy.dto.DishDto;
import copy.pojo.Dish;
import copy.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 小思
 * @Description //TODO
 * @Date $ $
 * @Param $
 * @return $
 **/

//@RestController
//@RequestMapping("/dish")
public class DishController {
//    @Autowired
//    private DishService dishService;
////    @Autowired
//    private RedisTemplate redisTemplate;

    /**
     * 新增套餐调用查询菜品数据 参数传菜品分类category_id 建议用菜品实体去接收 扩展性好
     * 后期发现参数还传了name 进行匹配搜索
     */
//    @GetMapping("/list")
    public R<List<Dish>> getbyId(Dish dish) {
        String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();//dish_17373737_1
        List<Dish>  o = (List<Dish>) redisTemplate.opsForValue().get(key);
        if (o != null) {
            return R.success(o);
        }
        LambdaQueryWrapper<Dish> s = new LambdaQueryWrapper<>();
        s.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId()).
                like(dish.getName() != null, Dish::getName, dish.getName());
        List<Dish> list = dishService.list(s);
        redisTemplate.opsForValue().set(key, list, 60, TimeUnit.MINUTES);
        return R.success(list);
    }

    /**
     * 不只是是put请求对菜品数据的处理一个新增一个更新 不能复用 属于逻辑没理清楚 所以再编写一个保存菜品和口为数据的接口
     * 修改菜品 下拉框加上图片上传下载 和最后的put请求保存菜品和口味
     * 保存口味数据的处理是先全部删除再添加
     */
//    @PutMapping
    public R<String> putsave(@RequestBody DishDto dishDto) {
        dishService.putsaveDishWithFlavors(dishDto);
        //执行了数据跟新 要及时清理缓存
        String key = "dish_" + dishDto.getCategoryId() + "_1";
        redisTemplate.delete(key);
        return R.success("ok");
    }

    /**
     * 菜品修改 先查右侧分页 然后调用文件上传下载
     * ranhou然后此接口查出这条菜品数据包含口味数据
     * 即根据dishID查询dish数据
     * 最后调保存
     */
//    @GetMapping("/{id}")
    public R<DishDto> savedishF(@PathVariable Long id) {
        String key = "dish_" + id + "_1";
        DishDto o = (DishDto) redisTemplate.opsForValue().get(key);
        if (o != null) {
            return R.success(o);
        }
        DishDto dishDto = dishService.qWithFlavorsBydishId(id);
        redisTemplate.opsForValue().set(key,dishDto,60,TimeUnit.MINUTES);
        return R.success(dishDto);
    }

    /**
     * 菜品分页查询 封装菜品分类
     */
//    @GetMapping("/page")
    public R<Page> dishpage(@RequestParam int page,
                            @RequestParam int pageSize, String name) {
       /* String keys = "page" + page + "_1";
        Page<DishDto> s = (Page<DishDto>) redisTemplate.opsForValue().get(keys);
        if (s != null) {
            return R.success(s);
        }*/
        Page<DishDto> dishDto = dishService.Ipage(page, pageSize, name);
//        redisTemplate.opsForValue().set(keys, dishDto, 60, TimeUnit.MINUTES);
        return R.success(dishDto);
    }

    /**
     * 新增菜品 除图片上传下载
     * 还需查分类
     * 和保存
     */
//    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavors(dishDto);
        return R.success("ok");
    }

    /**
     * 删除菜品数据
     */
//    @DeleteMapping
//    public R<String> delete(Long ids) {
//        dishService.removeById(ids);
//        return R.success("ojk");
//    }

    /**
     * 批量停售
     */

    @PostMapping("/status/0")
    public R<String> status0(@RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Dish::getStatus, 0).in(Dish::getId, ids);
        dishService.update(wrapper);
        return R.success("ok");
    }
    /**
     * 批量起售
     */
    @PostMapping("/status/1")
    public R<String> status1(@RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Dish::getStatus, 1).in(Dish::getId, ids);
        dishService.update(wrapper);
        return R.success("ok");
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
       /* LambdaQueryWrapper<Dish> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.in(Dish::getId, ids);
        dishService.remove(updateWrapper);*/
        dishService.removeByIds(ids);

        return R.success("");
    }
}
